﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Threading;

namespace CameraPrpject
{
    public partial class CameateThree : Form
    {
        private const int WM_USER = 0x400;
        private const int WS_CHILD = 0x40000000;
        private const int WS_VISIBLE = 0x10000000;
        private const int SWP_NOMOVE = 0x2;
        private const int SWP_NOZORDER = 0x4;
        private const int WM_CAP_START = WM_USER;
        private const int WM_CAP_STOP = WM_CAP_START + 68;
        private const int WM_CAP_SEQUENCE = WM_CAP_START + 62;
        private const int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;
        public CameateThree()
        {
            InitializeComponent();
        }
        private Camera wc;
        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_start_Click(object sender, EventArgs e)
        {
            this.wc = new Camera(this.panelPreview.Handle, this.panelPreview.Width, this.panelPreview.Height);
            this.wc.StartWebCam();
        }
        /// <summary>
        /// 断开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_stop_Click(object sender, EventArgs e)
        {
            Video.SendMessage(wc.lwndC, 0x40b, 0, 0);
            //Video.DestroyWindow(wc.lwndC);
            this.wc.CloseWebcam();
        }
        /// <summary>
        /// 截图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_cut_Click(object sender, EventArgs e)
        {
            try
            {
                Video.SendMessage(wc.lwndC, 0x41e, 0, 0);
                IDataObject obj1 = Clipboard.GetDataObject();
                if (obj1.GetDataPresent(typeof(Bitmap)))
                {
                    Image image1 = (Image)obj1.GetData(typeof(Bitmap));
                    SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
                    SaveFileDialog1.FileName = DateTime.Now.ToString("yyyyMMddhhmmss");
                    SaveFileDialog1.Filter = "Image Files(*.JPG;*.GIF)|*.JPG;*.GIF|All files (*.*)|*.*";
                    if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        image1.Save(SaveFileDialog1.FileName, ImageFormat.Bmp);
                    }
                }
            }
            catch
            {
            }
        }
        /// <summary>
        /// 监听
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_listen_Click(object sender, EventArgs e)
        {
            //txtchange();
            Thread ThreadFirst = new Thread(StarKinescope);//创建线程，传入线程执行的方法
            ThreadFirst.IsBackground = true;  //将一个线程设定成后台线程
            ThreadFirst.Start(new List<string>() { @"C:\Users\zhoulei\Desktop\mm\a.avi" });      //启动线程执行方法
        }
        /// <summary>
        ///开始 录制
        /// </summary>
        /// <param name="path">保存路径</param>
        private void StarKinescope(object path)
        {
            List<string> list = path as List<string>;//泛型集合
            string address = "";
            if (path != null)
            {
                foreach (string s in list)
                {
                    address = s;
                }
                IntPtr hBmp = Marshal.StringToHGlobalAnsi(address);
                Video.SendMessage(wc.lwndC, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, hBmp.ToInt32());
                Video.SendMessage(wc.lwndC, WM_CAP_SEQUENCE, 0, 0);
            }
        }
        /// <summary>
        /// 停止录制
        /// </summary>
        /// <param name="path"></param>
        public void StopKinescope()
        {
            Video.SendMessage(wc.lwndC, WM_CAP_STOP, 0, 0);
        }
        void txtchange()
        {
            for (int i = 0; i < 1000000000; i++)
            {
                //text_IP.Text = i.ToString();
            }
            MessageBox.Show("循环完成");
        }
    }
}
